package models

import "time"

type EnterpriseStaff struct {
	Id              int       `json:"id"`
	EnterpriseId    int       `json:"enterprise_id"`
	UserId          int       `json:"user_id"`
	Name            string    `json:"name"`
	Avatar          string    `json:"avatar"`
	Phone           string    `json:"phone"`
	Weixin          string    `json:"weixin"`
	Qq              string    `json:"qq"`
	Job             string    `json:"job"`
	DepartmentId    int       `json:"department_id"`
	Email           string    `json:"email"`
	EntryTime       time.Time `json:"entry_time"`
	Remarks         string    `json:"remarks"`
	IsAdministrator int       `json:"is_administrator"`
	IsAgree         int       `json:"is_agree"`
	PaymentPassword string    `json:"-"`
	CreatedAt       time.Time `json:"created_at"`
	UpdatedAt       time.Time `json:"updated_at"`
	Status          int       `json:"status"`
}

func (d *EnterpriseStaff) TableName() string {
	return "enterprise_staff"
}

func GetStaffList(page, limit, enterpriseId int, staffName, staffPhone, beginTime string) (interface{}, *Paginator) {
	var res []*struct {
		Id        int       `json:"id"`
		Name      string    `json:"name"`
		ComName   string    `json:"com_name"`
		Job       string    `json:"job"`
		IdentTime time.Time `json:"ident_time"`
		Phone     string    `json:"phone"`
	}
	m := DB.Table("enterprise_staff es ").Select("es.id,es.name,es.phone,es.job,e.name as com_name,e.ident_time").
		Joins("left join enterprise e on es.enterprise_id = e.id").Order("es.id DESC").Where("e.id = ?", enterpriseId)

	if staffName != "" {
		m = m.Where("es.name=?", staffName)
	}
	if staffPhone != "" {
		m = m.Where("es.phone=?", staffPhone)
	}
	if beginTime != "" {
		m = m.Where("e.ident_time > ?", beginTime)
	}

	var count int
	m.Count(&count)
	paginate := NewPage(page, limit, count)

	err := m.Offset(paginate.Offset).Limit(paginate.Limit).Scan(&res).Error
	if err != nil {
		return nil, nil
	}

	return res, &paginate
}

func StaffRechargesList(page, limit int, length, comName, staffPhone, beginTime string) (interface{}, *Paginator) {
	var res []*struct {
		Id                  int       `json:"id"`
		Name                string    `json:"name"`
		ComName             string    `json:"com_name"`
		Job                 string    `json:"job"`
		PayTime             time.Time `json:"pay_time"`
		Phone               string    `json:"phone"`
		VipFeeConfigComment string    `json:"vip_fee_config_comment"`
		Money               string    `json:"money"`
		OveredAt            time.Time `json:"overed_at"`
	}
	m := DB.Table("enterprise_staff_recharges sf ").Select("sf.id,es.name,es.phone,e.name as com_name,sf.pay_time,sf.vip_fee_config_comment,sf.money,sf.overed_at").
		Joins("left join enterprise e on sf.enterprise_id = e.id").
		Joins("left join enterprise_staff es on sf.staff_id = es.id").
		Order("sf.id DESC").Where("sf.is_pay = ?", 1)
	if length != "" {
		m = m.Where("sf.vip_fee_config_comment like ?", "%"+length+"%")
	}
	if comName != "" {
		m = m.Where("e.name like ? ", "%"+comName+"%")
	}
	if staffPhone != "" {
		m = m.Where("es.phone=?", staffPhone)
	}
	if beginTime != "" {
		m = m.Where("sf.pay_time > ?", beginTime)
	}

	var count int
	m.Count(&count)
	paginate := NewPage(page, limit, count)

	err := m.Offset(paginate.Offset).Limit(paginate.Limit).Scan(&res).Error
	if err != nil {
		return nil, nil
	}

	return res, &paginate
}

func (e *EnterpriseStaff) Count() int {

	count := 0
	DB.Model(EnterpriseStaff{}).Where("enterprise_id=?", e.EnterpriseId).Count(&count)

	return count
}
